// 用户分析
import { http } from "@/utils/http";

import { whReqBaseUrl as VITE_REQUEST_BASE_SAAS_API } from "@/api/common";

import { type basicUserQueryType } from "@/api/types";
import { useUserStoreHook } from "@/store/modules/user";

type userAnalysisType = {
  medicationManagementCount: number; //用药管理用户总数
  reservationServiceCount: number; //预约服务 (人次)
  newMedicationManagementCount: number; //新用户 (人)
  completeOrderCount: number; //完成取药用户（人次）
  regularMedicationCount: number; //规律取药用户 (人次)
};
type userAnalysisQueryDataType = basicUserQueryType & {
  startDate: string;
  endDate: string;
};
// 用户分析
export function getUserAnalysis(data: userAnalysisQueryDataType) {
  return http.get<userAnalysisType>(
    `${VITE_REQUEST_BASE_SAAS_API}/stat/supplyAnalysis`,
    {
      params: {
        ...data
      }
    }
  );
}

// 用户分析-趋势分析
export function getUserTrendAnalysis(data) {
  let queryUrl = "/stat/supplyAnalysis/trend";
  // const mock = localStorage.getItem("mock");

  if (useUserStoreHook().mock === "1") {
    queryUrl = "/stat/supplyTrend/";
  }
  return http.get<userAnalysisType>(
    `${VITE_REQUEST_BASE_SAAS_API}${queryUrl}`,
    {
      params: {
        ...data
      }
    }
  );
}

type warehousesType = {
  id: Number;
  warehouseName: String;
  address: String;
};

type warehousesResType = {
  total: number | undefined;
  list: Array<warehousesType> | null;
};

// 中心药房列表
export function getWarehousesList(data) {
  return http.get<warehousesResType>(
    `${VITE_REQUEST_BASE_SAAS_API}/warehouses`,
    {
      params: {
        ...data
      }
    }
  );
}

export function getWarehousesById(id) {
  return http.get<any>(`${VITE_REQUEST_BASE_SAAS_API}/warehouses/${id}`, {
    params: {}
  });
}

// 新增编辑中心药房

export function createAndUpdateWarehouses(data) {
  return http.postForm<any>(`${VITE_REQUEST_BASE_SAAS_API}/warehouses`, {
    ...data
  });
}

// 查询药品目录
export function getSalesCatalog() {
  return http.get<any>(`${VITE_REQUEST_BASE_SAAS_API}/salesCatalog`, {
    params: {}
  });
}

// 查询药品目录药品
export function getSalesDrugs(id, data) {
  return http.get<any>(
    `${VITE_REQUEST_BASE_SAAS_API}/salesCatalog/${id}/drugs`,
    {
      params: {
        ...data
      }
    }
  );
}

// 修改价格
export function changeDrugPrice(catalogId, data) {
  return http.postForm<any>(
    `${VITE_REQUEST_BASE_SAAS_API}/salesCatalog/${catalogId}/drugPrice`,
    {
      ...data
    }
  );
}

// 删除药品
export function deleteDrugApi(catalogId, data) {
  return http.postForm<any>(
    `${VITE_REQUEST_BASE_SAAS_API}/salesCatalog/${catalogId}/removeDrugs`,
    {
      ...data
    }
  );
}

// 添加药品
export function addDrugApi(catalogId, data) {
  return http.postForm<any>(
    `${VITE_REQUEST_BASE_SAAS_API}/salesCatalog/${catalogId}/addDrug`,
    {
      ...data
    }
  );
}

// 查询药品

export function searchCommonDrugsApi(data) {
  return http.get<any>(`${VITE_REQUEST_BASE_SAAS_API}/drugs`, {
    params: {
      ...data
    }
  });
}

// 订单查询

export function searchOrders(orderNo) {
  return http.get<any>(`${VITE_REQUEST_BASE_SAAS_API}/orders/${orderNo}`, {
    params: {}
  });
}

export function getAgentTypesApi() {
  return http.get<any>(`${VITE_REQUEST_BASE_SAAS_API}/hospitals/agencyTypes`, {
    params: {}
  });
}
